package com.bigstonecontainer.training.springboot.frm.server.core.controller;

import com.bigstonecontainer.training.springboot.frm.server.core.domain.LoginRequest;
import com.bigstonecontainer.training.springboot.frm.server.core.domain.LoginUser;
import com.bigstonecontainer.training.springboot.frm.server.core.domain.Result;
import com.bigstonecontainer.training.springboot.frm.server.core.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

/**
 * 认证控制器
 */
@RestController
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("/login")
    public Result<LoginUser> login(@RequestBody LoginRequest request) {
        LoginUser user = authService.authenticate(request.getUsername(), request.getPassword());
        return Result.success(user);
    }

    @PostMapping("/logout")
    public Result getHeaderInfo(@RequestHeader("Authorization") String token) {
        authService.logout(token);
        return Result.success();
    }
}
